const express = require("express")
const router = express.Router()
const jwt = require("jsonwebtoken")
const config = require("./util/config.js")
const SQLConnect = require("./SQLConnect.js")
const banners = require("./data/banner.json")
const newGoods = require("./data/newsGoods.json")
const recommendGoods = require("./data/recommendGoods.json")
const personStore = require("./data/personStore.json")
const suit = require("./data/suit.json")
const url = require("url");
const md5 = require("js-md5")

/**
 * banner接口地址
 */
router.get("/banner", (req, res) => {
    res.send(banners)
})

/**
 * 最新商品
 */
router.get("/newgoods", (req, res) => {
    res.send(newGoods)
})

/**
 * 推荐商品
 */
router.get("/recommendGoods", (req, res) => {
    const sql = "select * from goods";
    SQLConnect(sql, null, (result) => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result
            });
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            });
        }

    })
})

/**
 * 商品详情
 */
router.get("/goods/details", (req, res) => {
    var id = url.parse(req.url, true).query.id;
    const sql = "select * from goods where id=?";
    SQLConnect(sql, [id], (result) => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result[0]
            });
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            });
        }

    })
})



/**
 * 人才库
 */
router.get("/personStore", (req, res) => {
    res.send(personStore)
})

/**
 * 美业快讯
 */
router.get("/beauty", (req, res) => {
    var page = url.parse(req.url, true).query.page || 1;
    const sql = "select * from beauty limit 10 offset " + (page - 1) * 10;
    SQLConnect(sql, null, (result) => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: {
                    result: result
                }
            });
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            });
        }

    })
})

/**
 * 选址
 */
router.get("/place", (req, res) => {
    var page = url.parse(req.url, true).query.page || 1;
    var location = url.parse(req.url, true).query.location
    var price = url.parse(req.url, true).query.price
    const sql = "select * from place limit 10 offset " + (page - 1) * 10;
    SQLConnect(sql, null, (result) => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: {
                    result: result
                }
            });
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            });
        }

    })
})

/**
 * 精选套装
 */
router.get("/suit", (req, res) => {
    res.send(suit)
})

/**
 * 人才页面数据
 */
router.get("/person/list", (req, res) => {
    var page = url.parse(req.url, true).query.page || 1;
    var location = url.parse(req.url, true).query.location
    var price = url.parse(req.url, true).query.price
    const sql = "select * from person limit 4 offset " + (page - 1) * 4;
    SQLConnect(sql, null, (result) => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: {
                    result: result
                }
            });
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            });
        }

    })
})


/**
 * 人才详情
 */
router.get("/person/detail", (req, res) => {
    var id = url.parse(req.url, true).query.id || 1;
    const sql = "select * from person where id=?";
    var arr = [id]
    SQLConnect(sql, arr, (result) => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result[0]
            });
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            });
        }
    })
})

/**
 * 全部订单
 */
router.get("/order/all", (req, res) => {
    const sql = "select * from allorder";
    SQLConnect(sql, null, (result) => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: {
                    result: result
                }
            });
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            });
        }

    })
})


/**
 * 订单类型
 *  1:待支付
 *  2:待收货
 *  3:待评价
 */
router.get("/order/type", (req, res) => {
    var type = url.parse(req.url, true).query.type;
    const sql = "select * from allorder where status=?";
    var arr = [type]
    SQLConnect(sql, arr, (result) => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result
            });
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            });
        }
    })
})

/**
 * 选址详情
 */
router.get("/place/details", (req, res) => {
    var id = url.parse(req.url, true).query.id;
    const sql = "select * from place where id=?";
    var arr = [id]
    SQLConnect(sql, arr, (result) => {
        if (result.length > 0) {
            res.send({
                status: 200,
                data: result[0]
            });
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            });
        }
    })
})

/**
 * 评论
 */
router.get("/comment/goods", (req, res) => {
    var id = url.parse(req.url, true).query.id;
    const sql = "select * from comment where id=?";
    var arr = [id]
    SQLConnect(sql, arr, (result) => {
        if (result.length > 0) {
            /**
             * 字符串拆分为数组
             */
            let images = result[0].images.split(",")
            res.send({
                status: 200,
                data: [{
                    images,
                    id: result[0].id,
                    icon: result[0].icon,
                    name: result[0].name,
                    rate: result[0].rate,
                    time: result[0].time,
                    text: result[0].text
                }]
            });
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            });
        }
    })
})

/**
 * 全部评论
 */
router.get("/comment", (req, res) => {
    const sql = "select * from comment";
    SQLConnect(sql, null, (result) => {
        if (result.length > 0) {
            let currentResult = [];
            for (let i = 0; i < result.length; i++) {
                let images = result[i].images.split(",")
                let temp = {
                    id: result[i].id,
                    icon: result[i].icon,
                    name: result[i].name,
                    rate: result[i].rate,
                    time: result[i].time,
                    text: result[i].text,
                    images
                }
                currentResult.push(temp)
            }
            res.send({
                status: 200,
                data: currentResult
            });
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            });
        }
    })
})

/**
 * 登录
 */
router.post("/login", (req, res) => {
    const { password, phone, verify } = req.body
    if (verify == md5("iwenwiki")) {
        const sql = "select * from user where `phone`=? AND `password`=?";
        const arr = [phone, password]
        SQLConnect(sql, arr, (result) => {
            if (result.length > 0) {
                const token = jwt.sign({
                    id: result[0].id,
                    username: result[0].username
                }, config.jwtSecret)
                res.send({
                    status: 200,
                    username: phone,
                    token
                })
            } else {
                res.send({
                    status: 500,
                    msg: "用户名密码错误"
                });
            }
        })
    }else{
        res.send({
            status:500,
            msg:"验证失败"
        })
    }
})

/**
 * 注册
 */
router.post("/register", (req, res) => {
    const { password, phone } = req.body
    const sql = "insert into user values (null,'',?,?)";
    const arr = [password, phone]
    SQLConnect(sql, arr, (result) => {
        if (result.affectedRows) {
            res.send({
                status: 200,
                message: "注册成功"
            })
        } else {
            res.send({
                status: 500,
                message: "注册失败"
            })
        }
    })
})



module.exports = router;